package com.ruoyi.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CCategory;
import com.ruoyi.system.service.ICCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 资讯类别Controller
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Controller
@RequestMapping("/system/newsCategory")
public class CCategoryController extends BaseController
{
    private String prefix = "system/newsCategory";

    @Autowired
    private ICCategoryService cCategoryService;

    @RequiresPermissions("system:newsCategory:view")
    @GetMapping()
    public String newsCategory()
    {
        return prefix + "/newsCategory";
    }

    /**
     * 查询资讯类别树列表
     */
    @RequiresPermissions("system:newsCategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CCategory> list(CCategory cCategory)
    {
        List<CCategory> list = cCategoryService.selectCCategoryList(cCategory);
        return list;
    }

    /**
     * 导出资讯类别列表
     */
    @RequiresPermissions("system:newsCategory:export")
    @Log(title = "资讯类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CCategory cCategory)
    {
        List<CCategory> list = cCategoryService.selectCCategoryList(cCategory);
        ExcelUtil<CCategory> util = new ExcelUtil<CCategory>(CCategory.class);
        return util.exportExcel(list, "资讯类别数据");
    }

    /**
     * 新增资讯类别
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cCategory", cCategoryService.selectCCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存资讯类别
     */
    @RequiresPermissions("system:newsCategory:add")
    @Log(title = "资讯类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CCategory cCategory)
    {
        return toAjax(cCategoryService.insertCCategory(cCategory));
    }

    /**
     * 修改资讯类别
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CCategory cCategory = null;
        if(id == null || id ==  -1){
            cCategory = new CCategory();
        }else{
            cCategory = cCategoryService.selectCCategoryById(id);
        }
        mmap.put("cCategory", cCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存资讯类别
     */
    @RequiresPermissions("system:newsCategory:edit")
    @Log(title = "资讯类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CCategory cCategory)
    {
        return toAjax(cCategoryService.updateCCategory(cCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:newsCategory:remove")
    @Log(title = "资讯类别", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cCategoryService.deleteCCategoryById(id));
    }

    /**
     * 选择资讯类别树
     */
    @GetMapping(value = { "/selectNewsCategoryTree/{id}", "/selectNewsCategoryTree/" })
    public String selectNewsCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cCategory", cCategoryService.selectCCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载资讯类别树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cCategoryService.selectCCategoryTree();
        return ztrees;
    }
}
